home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / LHX / SHUF.C < prev    next >
C/C++ Source or Header  |  1993-11-11  |  4KB  |  221 lines

  1. /***********************************************************
  2.         shuf.c -- extract static Huffman coding
  3. ***********************************************************/
  4. #include    "lh386.h"
  5.  
  6. #include    <stdio.h>
  7. #include    <stdlib.h>
  8. #include    "slidehuf.h"
  9.  
  10. #ifdef    __HIGHC__
  11. #    pragma    On(Print_reg_vars);
  12. #    pragma    On(Align_labels);
  13. #endif
  14.  
  15. #ifdef    __HIGHC__
  16. #    ifndef    PASCAL
  17. #        define    PASCAL    (_DEFAULT_CALLING_CONVENTION | _CALLEE_POPS_STACK)
  18. #    endif
  19. #endif
  20.  
  21. #ifdef    __HIGHC__
  22. #    pragma    Calling_convention(PASCAL);
  23. #endif
  24. static void ready_made(int method);
  25. static void read_tree_c(void);
  26. static void read_tree_p(void);
  27. #ifdef    __HIGHC__
  28. #    pragma    Calling_convention();
  29. #endif
  30.  
  31.  
  32. #define N1 286                    /* alphabet size */
  33. #define N2 (2 * N1 - 1)         /* # of nodes in Huffman tree */
  34. #define EXTRABITS 8                /* >= log2(F-THRESHOLD+258-N1) */
  35. #define BUFBITS  16             /* >= log2(MAXBUF) */
  36. #define LENFIELD  4             /* bit size of length field for tree output */
  37. #define NP (8 * 1024 / 64)
  38. #define NP2 (NP * 2 - 1)
  39.  
  40. static unsigned int np;
  41.  
  42. void        decode_start_st0(void)
  43. {
  44.     n_max = 286;
  45.     maxmatch = MAXMATCH;
  46.     init_getbits();
  47.     np = 1 << (MAX_DICBIT - 6);
  48. }
  49.  
  50. void        encode_p_st0(unsigned short j)
  51. {
  52.     unsigned short i;
  53.  
  54.     i = j >> 6;
  55.     putcode(pt_len[i], pt_code[i]);
  56.     putbits(6, j & 0x3f);
  57. }
  58.  
  59. int         fixed[2][16] =
  60. {
  61.     {3, 0x01, 0x04, 0x0c, 0x18, 0x30, 0},        /* old compatible */
  62.     {2, 0x01, 0x01, 0x03, 0x06, 0x0D, 0x1F, 0x4E, 0}    /* 8K buf */
  63. };
  64.  
  65. static void ready_made(int method)
  66. {
  67.     REG int     i, j;
  68.     uint        code, weight;
  69.     REG int     *tbl;
  70.  
  71.     tbl = fixed[method];
  72.     j = *tbl++;
  73.     weight = 1 << (16 - j);
  74.     code = 0;
  75.     for (i = 0; i < np; i++)
  76.     {
  77.         while (*tbl == i)
  78.         {
  79.             j++;
  80.             tbl++;
  81.             weight >>= 1;
  82.         }
  83.         pt_len[i] = j;
  84.         pt_code[i] = code;
  85.         code += weight;
  86.     }
  87. }
  88.  
  89. void        encode_start_fix(void)
  90. {
  91.     n_max = 314;
  92.     maxmatch = 60;
  93.     np = 1 << (12 - 6);
  94.     init_putbits();
  95.     start_c_dyn();
  96.     ready_made(0);
  97. }
  98.  
  99. static void read_tree_c(void)
  100. /* read tree from file */
  101. {
  102.     REG int     i, c;
  103.  
  104.     i = 0;
  105.     while (i < N1)
  106.     {
  107.         if (getbits(1))
  108.             c_len[i] = getbits(LENFIELD) + 1;
  109.         else
  110.             c_len[i] = 0;
  111.         if (++i == 3 && c_len[0] == 1 && c_len[1] == 1 && c_len[2] == 1)
  112.         {
  113.             c = getbits(CBIT);
  114.             for (i = 0; i < N1; i++)
  115.                 c_len[i] = 0;
  116.             for (i = 0; i < 4096; i++)
  117.                 c_table[i] = c;
  118.             return;
  119.         }
  120.     }
  121.     make_table(N1, c_len, 12, c_table);
  122. }
  123.  
  124. static void read_tree_p(void)
  125. /* read tree from file */
  126. {
  127.     REG int     i, c;
  128.  
  129.     i = 0;
  130.     while (i < NP)
  131.     {
  132.         pt_len[i] = getbits(LENFIELD);
  133.         if (++i == 3 && pt_len[0] == 1 && pt_len[1] == 1 && pt_len[2] == 1)
  134.         {
  135.             c = getbits(MAX_DICBIT - 6);
  136.             for (i = 0; i < NP; i++)
  137.                 pt_len[i] = 0;
  138.             for (i = 0; i < 256; i++)
  139.                 pt_table[i] = c;
  140.             return;
  141.         }
  142.     }
  143. }
  144.  
  145. void        decode_start_fix(void)
  146. {
  147.     n_max = 314;
  148.     maxmatch = 60;
  149.     init_getbits();
  150.     np = 1 << (12 - 6);
  151.     start_c_dyn();
  152.     ready_made(0);
  153.     make_table(np, pt_len, 8, pt_table);
  154. }
  155.  
  156. ushort        decode_c_st0(void)
  157. {
  158.     REG int     i, j;
  159.     static unsigned short blocksize = 0;
  160.  
  161.     if (blocksize == 0)
  162.     {                            /* read block head */
  163.         blocksize = getbits(BUFBITS);    /* read block blocksize */
  164.         read_tree_c();
  165.         if (getbits(1))
  166.         {
  167.             read_tree_p();
  168.         } else
  169.         {
  170.             ready_made(1);
  171.         }
  172.         make_table(NP, pt_len, 8, pt_table);
  173.     }
  174.     blocksize--;
  175.     j = c_table[bitbuf >> 4];
  176.     if (j < N1)
  177.         fillbuf(c_len[j]);
  178.     else
  179.     {
  180.         fillbuf(12);
  181.         i = bitbuf;
  182.         do
  183.         {
  184.             if ((signed short) i < 0)
  185.                 j = right[j];
  186.             else
  187.                 j = left[j];
  188.             i <<= 1;
  189.         } while (j >= N1);
  190.         fillbuf(c_len[j] - 12);
  191.     }
  192.     if (j == N1 - 1)
  193.         j += getbits(EXTRABITS);
  194.     return j;
  195. }
  196.  
  197. ushort        decode_p_st0(void)
  198. {
  199.     REG int     i, j;
  200.  
  201.     j = pt_table[bitbuf >> 8];
  202.     if (j < np)
  203.     {
  204.         fillbuf(pt_len[j]);
  205.     } else
  206.     {
  207.         fillbuf(8);
  208.         i = bitbuf;
  209.         do
  210.         {
  211.             if ((signed short) i < 0)
  212.                 j = right[j];
  213.             else
  214.                 j = left[j];
  215.             i <<= 1;
  216.         } while (j >= np);
  217.         fillbuf(pt_len[j] - 8);
  218.     }
  219.     return (j << 6) + getbits(6);
  220. }
  221.